package com.example.controller;

import com.example.Utils.DingText;
import com.example.pojo.Result;
import com.example.pojo.baseComponentPojo;
import com.example.pojo.productModelListPojo;
import com.example.service.baseComponentService;
import com.example.service.componentBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ClassName:baseComponentController
 * Description:
 *
 * @Author 赵永峰
 * @Create 2024/7/11 14:45
 * @Version 1.0
 */
@RestController
@Slf4j
public class baseComponentController {
    @Autowired
    private baseComponentService baseComponentService;
    @Autowired
    private componentBookService componentBookService;
    @PostMapping("/insertBaseComponent")
    //插入一条新的记录
    public Result insertBaseComponent(@RequestBody baseComponentPojo baseComponent){
        //向基础表中插入数据
        baseComponentService.insertOne(baseComponent);
        //向零部件作业指导书中插入数据：component_book
        //筛选出最新写入的数据,利用的是Id最大筛选
        baseComponentPojo baseComponentPojo=baseComponentService.queryByIdMax();
        //对零部件作业指导书管理中的数据进行修订,依据是baseId
        componentBookService.insertOne(baseComponentPojo);
        return Result.success();
    }
    @PostMapping("/queryAllBaseComponent")
    //查询所有的数据
    public Result queryAllBaseComponent(){
        List<baseComponentPojo> baseComponentPojoList=baseComponentService.queryAll();
        return Result.success(baseComponentPojoList);
    }
    //对零部件信息进行编辑
    @PostMapping("/editBaseComponent")
    public Result editBaseComponent(@RequestBody baseComponentPojo baseComponent){
        //对基础表格信息进行修改
        baseComponentService.editOne(baseComponent);
        componentBookService.editByBaseId(baseComponent);
        return Result.success();
    }
    //辅助函数：对零部件信息进行调用,调用所有的信息
    @PostMapping("/inquiryProductListBaseComponentAll")
    public Result inquiryProductListBaseComponentAll(){
        List<productModelListPojo> pojos=baseComponentService.inquiryProductListBaseComponentAll();
        return Result.success(pojos);
    }
    //辅助函数，处理一个文件，新增一个文件、替换一个文件、删除一个文件
    @Value("${file.upload-dir}")
    private String saveDir;
    @PostMapping("editOneFileBase")
    public Result editOneFileBase(
            @RequestParam(value = "newFile",required = false) MultipartFile newFile,
            @RequestParam(value = "newFileName",required = false) String newFileName,
            @RequestParam(value = "oldFileName",required = false) String oldFileName,
            @RequestParam(value = "type",required = false) int type){
        System.out.println(type);
        //新增一个文件
        if (type==1){
            //把文件插入
            try {
                if (!newFile.isEmpty()) {
                    String filePath = saveDir + newFileName;
                    File dest = new File(filePath);
                    // 将文件保存到服务器上的指定位置
                    newFile.transferTo(dest);
                    return Result.success("操作成功");
                }
                else {
                    return Result.success("没有收到文件，请检查");
                }
            }
            catch (IOException e) {
                return Result.success("文件保存出错，请联系管理员");
            }
        }
        //删除老文件，使用新文件替换老文件
        else if (type==2){
            //1.删除老文件
            String filePath =saveDir + oldFileName;
            // 使用 Paths 类的静态方法 get 来根据给定的文件路径字符串 filePath 创建一个 Path 对象
            Path path = Paths.get(filePath);
            try {
                Files.delete(path);
                return Result.success("操作成功");
            }
            catch (IOException e) {
                System.err.println("删除文件时出错: " + e.getMessage());
            }
            //2.插入新文件
            try {
                if (!newFile.isEmpty()) {
                    filePath = saveDir + newFileName;
                    File dest = new File(filePath);
                    // 将文件保存到服务器上的指定位置
                    newFile.transferTo(dest);
                }
            }
            catch (IOException e) {
                return Result.success("文件删除出错");
            }
            return Result.success("数据修改成功");
        }
        //删除文件，并且修改数据
        else if (type==3){
            System.out.println("daozhelile");
            //1.删除老文件
            String filePath =saveDir + oldFileName;
            // 使用 Paths 类的静态方法 get 来根据给定的文件路径字符串 filePath 创建一个 Path 对象
            Path path = Paths.get(filePath);
            try {
                Files.delete(path);
                return Result.success("操作成功");
            }
            catch (IOException e) {
                return Result.success("删除文件时出错: " + e.getMessage());
            }
        }
        else {
            return Result.success("传入的类型不正确");
        }
    }

    //下载文件
    @GetMapping("/downloadFileBaseComponent")
    public Result downloadFileBaseComponent(@RequestParam("fileName") String fileName){
        byte[] file= baseComponentService.readFile(fileName);
        System.out.println(fileName);
        return Result.success(file);
    }
    //向钉钉发送消息
    @PostMapping("/dingMessageBasic")
    public Result dingMessageBasic(@RequestBody Map<String,Object> map){
        //分离数据，发送钉钉信息
        String dingId = (String) map.get("dingId");
        String dingIdentifyId=(String) map.get("dingIdentifyId");
        String text = (String) map.get("text");
        String link = (String) map.get("link");
        int type = (int) map.get("type");
        if (type == 1){
            System.out.println("发送文本");
            List<String> list=new ArrayList<>();
            list.add("GW3000687");
            DingText.sendMessageByText(
                    text,list,
                    false,
                    //机器人的识别号
                    "SECa6d4f3f3f069b486ba7cd760fe9467a7fffa612f214e193ff68fa84d3b397026",
                    //机器人的地址号
                    "https://oapi.dingtalk.com/robot/send?access_token=97a109e2c40baec56d46127530be7e4444fc15359349e4987b62883c8abdd002");
        }
        else if (type == 2){
            System.out.println("发送文本和连接");
            List<String> list=new ArrayList<>();
            list.add("GW3000687");
            DingText.sendMessageByText(
                    "第一个文本",list,
                    false,
                    //机器人的识别号
                    "SECa6d4f3f3f069b486ba7cd760fe9467a7fffa612f214e193ff68fa84d3b397026",
                    //机器人的地址号
                    "https://oapi.dingtalk.com/robot/send?access_token=97a109e2c40baec56d46127530be7e4444fc15359349e4987b62883c8abdd002");
        }
        return Result.success("信息发送成功");
    }
}
